package com.pyg.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pyg.manager.service.BrandService;
import com.pyg.pojo.PageResult;
import com.pyg.pojo.PygResult;
import com.pyg.pojo.TbBrand;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController  {

    @Reference(timeout = 1000000)
    private BrandService brandService;

    /**
     * 需求：查询所有品牌
     */
    @RequestMapping("findAll")
    public List<TbBrand> findAll() {
        //调用远程服务对象方法
        List<TbBrand> brandList = brandService.findAll();
        return brandList;
    }

    @RequestMapping("findByPage/{page}/{rows}")
    public PageResult findByPage(@PathVariable Integer page,@PathVariable Integer rows){
        PageResult result = brandService.findByPage(page, rows);
        return result;
    }

    @RequestMapping("add")
    public PygResult add(@RequestBody TbBrand brand){
        try {
            brandService.add(brand);
            return new PygResult(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"添加失败");
        }
    }

    @RequestMapping("findOne/{id}")
    public TbBrand findOne(@PathVariable Long id){
        return brandService.findOne(id);
    }

    @RequestMapping("update")
    public PygResult update(@RequestBody TbBrand brand){
        try {
            brandService.update(brand);
            return new PygResult(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"修改失败");
        }
    }

    @RequestMapping("delete")
    public PygResult delete(Long[] ids){
        try {
            brandService.delete(ids);
            return new PygResult(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"删除失败");
        }
    }

    @RequestMapping("findBrandList")
    public List<Map> findBrandList(){
        return brandService.findBrandList();
    }
}
